Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly infer x: SomeEnum; x.name as union of literal names #18797

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

sobolevn
Copy link
Member

Closes #18786

Copy link
Collaborator

@A5rocks A5rocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me

This comment has been minimized.

sobolevn added a commit to sobolevn/psycopg that referenced this pull request Mar 13, 2025
Hi! I am working on mypy in python/mypy#18797

And I've noticed a small thing in your code. Ideally, `SomeEnum.Item.name` should be inferenced as `Literal[NAME_ONE, NAME_TWO, ...]`

But, since you later modify `status` (which is the name of a enum item), it should be explicitly annotated as `str`, not `Literal`.
@sobolevn
Copy link
Member Author

I opened psycopg/psycopg#1023 to fix a "problem" is psycopg.

This comment has been minimized.

@TeamSpen210
Copy link
Contributor

Should this also handle unions of literals? I notice if you narrow with is, it'll switch back to str again. Similarly if you explicitly annotate with Literal[SomeEnum.field], or a union of two enums.

@sobolevn
Copy link
Member Author

@TeamSpen210 please, share your examples, I would be happy to support them as well :)

@TeamSpen210
Copy link
Contributor

Here's an example of both:

class SomeEnum(Enum):
    A = 0
    B = 1
    C = 2

class AnotherEnum(Enum):
    ONE = 1
    TWO = 2
    THREE = 3

def func(value: SomeEnum, combo: SomeEnum | Literal[AnotherEnum.ONE, AnotherEnum.THREE]):
    reveal_type(value.name)  # Literal["A", "B", "C"]
    assert value is not SomeEnum.A
    reveal_type(value.name)  # Just str, but should be Literal["B", "C"]?
    # also str, but could be Literal["A", "B", "C", "ONE", "THREE"]
    reveal_type(combo.name)

Second is a bit less likely, but seems reasonable to support?

@sobolevn
Copy link
Member Author

Ok, I see, supporting Literal[AnotherEnum.ONE, AnotherEnum.THREE].name seems reasonable to me. Literal values should produce other literals. But, SomeEnum | Literal[AnotherEnum.ONE, AnotherEnum.THREE] does not. It should be left as str, because looks like we have two different enums here. It might have duplicates, etc. Also, the use-case is not very common.

dvarrazzo pushed a commit to sobolevn/psycopg that referenced this pull request Mar 14, 2025
Currently enum names are strings, but this is being changed to become
Literal. When this will happen, the type inferred will not be valid
anymore, as we mutate it and manipulate it as string in the function.

See python/mypy#18797
@sobolevn
Copy link
Member Author

Done!

Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enum .name property has str type
3 participants